//
//  SelectCountryVC.m
//  ImGuider
//
//  Created by llt on 2017/4/24.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "SelectCountryVC.h"

@interface SelectCountryVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSDictionary *countryDict;
@property (nonatomic, strong) NSArray *keyArray;


@end

@implementation SelectCountryVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup];
}

- (void)setup {
    
    self.title = MYLocalizedString(@"REGISTER_PLACE", nil);
    
    self.isHiddenPlayStatusButton = YES;
    
    [self rightBarButton:MYLocalizedString(@"PUBLIC_CANCEL", nil) color:[UIColor blackColor] selector:@selector(cancelBtnClick)];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    if (![CountryManager sharedManager].allcountry) {
        
        [ActivityIndicatorView showHUDAddedTo:self.view backgroundColor:[UIColor whiteColor]];
        @weakify(self);
        [NetAPIManager getAllcountryWithBlock:^(NSArray *allcountry, NSError *error) {
            @strongify(self);
            [ActivityIndicatorView hideHUDAddedTo:self.view];
            [CountryManager sharedManager].allcountry = allcountry;
            
            [self setupData];
        }];
        
        return;
    }
    
    [self setupData];
}

- (void)setupData {
    
    self.keyArray = [[CountryManager sharedManager] getAllCountryIdxArray];
    self.countryDict = [[CountryManager sharedManager] getAllCountryIdxToCitiesDict];
    [self.tableView reloadData];
}

- (void)showWithVC:(UIViewController *)vc {
    
    [vc presentViewController:[[RTRootNavigationController alloc] initWithRootViewController:self] animated:YES completion:nil];
}

- (void)cancelBtnClick {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark -UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.keyArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSArray *countryArray = self.countryDict[self.keyArray[section]];
    return countryArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"countryCell"];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"countryCell"];
        cell.textLabel.font = Font(12);
        cell.detailTextLabel.font = Font(11);
    }
    
    NSArray *countryArray = self.countryDict[self.keyArray[indexPath.section]];
    Country *country = countryArray[indexPath.row];
    
    cell.textLabel.text = country.smartName;
    
    if (!_isOnlyCity) {
        cell.detailTextLabel.text = country.code;
    }
    return cell;
}

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    return self.keyArray[section];
}

- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    
    return self.keyArray;
}

#pragma mark -UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSArray *countryArray = self.countryDict[self.keyArray[indexPath.section]];
    Country *country = countryArray[indexPath.row];
    [_delegate selectedCountry:country];
    [self cancelBtnClick];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

